package io.renren.common.utils;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("resource")
public class ImpUtils {
    private static final String XLS = "xls";
    private static final String XLSK = "xlsx";//常量值

    public  Map<String, Object> importExcel(MultipartFile file,Map<String,Object> param) throws Exception {

        Map<String, Object> rsultMap = new HashMap<>();
        Map<String,Object> re=new HashMap<String,Object>();
        Workbook workbook = null;
        String fileName = file.getOriginalFilename();
        if(fileName.endsWith(XLS)) {
            //2003
            try {
                workbook = new HSSFWorkbook(file.getInputStream());
            } catch (Exception e) {
                e.printStackTrace( );
            }

        }else if(fileName.endsWith(XLSK)) {
            try {
                //2007
                workbook = new XSSFWorkbook(file.getInputStream());
            } catch (Exception e) {
                e.printStackTrace( );
            }
        }else {
            throw new Exception("文件不是Excel文件");
        }
        Sheet sheet = workbook.getSheet("Sheet1");
        int rows = sheet.getLastRowNum();//指定行数。一共多少+
        if(rows==0) {
            throw new Exception("请填写行数");
        }


        /** *******************************************具体业务处理***************************************************/
        re=businessDo(sheet,rows,param);
        /** *******************************************具体业务处理***************************************************/

        try {
            rsultMap.put("code", 200);  // 成功
            rsultMap.put("msg", "导入数据成功");
            int errorSize=Integer.parseInt(re.get("errorSize").toString());  //  失败条数
            String errorMsg=re.get("errorMsg").toString();
            String text=re.get("text").toString();
            if(errorSize>0){
                rsultMap.put("code", 505);
                rsultMap.put("msg",errorMsg+":" +text);

            }

        } catch (Exception e) {
            rsultMap.put("code", 505);
            rsultMap.put("msg", "导入数据异常");
        }

        return rsultMap;
    }


    //获取Cell内容
    public String getCellValue(Cell cell) {
        String value = "";
        if(cell != null) {
            //以下是判断数据的类型
            switch (cell.getCellType()) {
                case HSSFCell.CELL_TYPE_NUMERIC://数字
                    value = cell.getNumericCellValue() + "";
                    if(HSSFDateUtil.isCellDateFormatted(cell)) {
                        Date date = cell.getDateCellValue();
                        if(date != null) {
                            value = new SimpleDateFormat("yyyy-MM-dd").format(date);
                        }else {
                            value = "";
                        }
                    }else {
                        value = new DecimalFormat("0").format(cell.getNumericCellValue());
                    }
                    break;
                case HSSFCell.CELL_TYPE_STRING: //字符串
                    value = cell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN: //boolean
                    value = cell.getBooleanCellValue() + "";
                    break;
                case HSSFCell.CELL_TYPE_FORMULA: //公式
                    value = cell.getCellFormula() + "";
                    break;
                case HSSFCell.CELL_TYPE_BLANK: //空值
                    value = "";
                    break;
                case HSSFCell.CELL_TYPE_ERROR: //故障
                    value = "非法字符";
                    break;
                default:
                    value = "未知类型";
                    break;
            }
        }
        return value.trim();
    }

    /***
     * 处理业务
     */
    public   Map<String,Object>  businessDo(Sheet sheet,int  len,Map<String,Object> param){
           return null;
    }
}
